package com.sinodata.bsm.center.web;

import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.sinodata.bsm.center.bean.WorkEnv;
import com.sinodata.bsm.center.cache.CacheFactory;
import com.sinodata.bsm.center.engine.notify.Messenger;
import com.sinodata.bsm.center.engine.probe.ProbeStatusManager;
import com.sinodata.bsm.center.message.ProbeRequestListener;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.center.util.system.SystemContext;
import com.sinodata.bsm.common.message.BsmActiveMQFactory;

/**
 * 
 * <p>
 * Description: BSM系统启动监听
 * </p>
 * 
 * @author liulibo
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * 2012-5-2 PM 4:39:31 liulibo 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */
public class BsmSystemListener implements ServletContextListener {

    private ServletContext context = null;

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        this.context = null;
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.context = event.getServletContext();
        String realPath = context.getRealPath("/");
        SystemContext.setProperty("AppContextPath", realPath);
        String file = context.getInitParameter("log4jConfigLocation");
        Logger logger = null;
        try {
            PropertyConfigurator.configure(realPath + file);
            logger = Logger.getLogger(BsmSystemListener.class.getName());
        } catch (Exception ex) {
            logger.error("[ERROR]>>>>>Log4j Init Error.");
        }
        SpringContextHolder.getBean(WorkEnv.class).setRealPath(realPath);
        String loginUrl = SpringContextHolder.getBean(WorkEnv.class).getLoginUrl();
        System.setProperty("login.url", loginUrl);

        //-------------------------
        SpringContextHolder.getBean(CacheFactory.class).initCache();//缓存所有数据对象 
        //        EventEngine.start();//启动事件引擎
        //        NotifyEngine.start();//启动通知引擎
        //        EventAutoConfirmCenter.getInstance().start();//启动事件自动确认引擎
        try {
            Properties prop = new Properties();
            prop.put("jms.url", SpringContextHolder.getBean(WorkEnv.class).getActivemqUrl());
            BsmActiveMQFactory.getInstance().init(prop);
        } catch (Exception e) {
            logger.error("[ERROR]>>>>>Failed to read activeMq conf", e);
            return;
        }

        ProbeStatusManager.getInstance();//启动Probe轮巡
        ProbeRequestListener.getInstance();//启动同步队列监听
        Messenger.getInstance().init();//启动异步队列监听
        //        ResPauseEngine.getInstance().start();//启动资源暂停引擎
        //        EventMessageListener.init(); // 启动集成事件消息监听
        //        TrapMessageListener.init(); //启动TRAP消息监听

        //-------------------
    }
}